package echotest

import (
	"net/http"
	// "strconv"
	"echotest/manager"
	"github.com/donnie4w/go-logger/logger"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
)

func logInit() {
	log_dir := "/var/log/"
	log_file := "hllyun.log"
	logger.SetConsole(false)
	logger.SetLevelFile(logger.INFO, log_dir, log_file)
	logger.SetLevelFile(logger.WARN, log_dir, log_file)
	logger.SetLevelFile(logger.ERROR, log_dir, log_file)
}

func updateMsg(a echo.Context) error {

	logger.Info("echo update start~~~")
	return a.String(http.StatusOK, "Update Successful!\n")
}

func getAllParam(c echo.Context) error {
	logger.Info("echo get all params start~~~")
	// param, _ := strconv.Atoi(c.Param("vname"))
	param := c.Request()
	logger.Info("*******param is")
	logger.Info(param)
	return c.String(http.StatusOK, "Update Successful!\n")
}

func getAllVolumes(c echo.Context) error {
	result := manager.GetVolumes()
	return c.String(200, result)
}

func getArray(c echo.Context) (slice []int) {
	result := manager.GetArray()
	// return c.String(200, result)
	return result
}

func EchoTest() {
	logInit()
	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	// Route => handler
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, World!\n")
	})
	e.GET("/saybye", func(c echo.Context) error {
		return c.String(http.StatusOK, "Bye Bye~~\n")
	})

	e.PUT("/saybye", updateMsg)

	e.PUT("/param", getAllParam)

	e.GET("/api/v1.0/storage/volumes", getAllVolumes)
	// e.GET("/api/v1.0/test/array", getArray)

	// Start server
	e.Logger.Fatal(e.Start(":1323"))
}
